package com.example.mainstream.fragment;

import android.content.Intent;
import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.example.shopapp.App;
import com.example.shopapp.R;
import com.example.shopapp.adapter.OnOrderListener;
import com.example.shopapp.adapter.OrderAdapter;
import com.example.shopapp.bean.OrderBean;
import com.example.shopapp.database.OrderBeanDBUtils;
import com.example.shopapp.databinding.FragmentUserOrderBinding;
import com.example.shopapp.ui.user.OrderDetailActivity;
import com.example.shopapp.widget.MyItemDecoration;

import java.util.Collections;
import java.util.List;

import cn.project.base.baseui.BaseNormalFragment;

public class RecieveFragment extends BaseNormalFragment<FragmentUserOrderBinding> implements OnOrderListener {

    OrderAdapter orderAdapter;
    @Override
    protected int getLayoutId() {
        return R.layout.fragment_user_order;
    }

    @Override
    protected void initData() {
        orderAdapter = new OrderAdapter(getActivity(),this);
        mBinding.ryOrder.setLayoutManager(new LinearLayoutManager(getActivity()));
        mBinding.ryOrder.addItemDecoration(new MyItemDecoration(10));
        mBinding.ryOrder.setAdapter(orderAdapter);

        List<OrderBean> orderBeanList = OrderBeanDBUtils.getInstance(getActivity()).getListByUserid(App.getInstance().user.getUser_id(),"2");
        if (orderBeanList ==null || orderBeanList.size() ==0){
            mBinding.rlEmpty.setVisibility(View.VISIBLE);
        }else {
            Collections.reverse(orderBeanList);
            mBinding.rlEmpty.setVisibility(View.GONE);
            orderAdapter.setNewData(orderBeanList);
            orderAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onEventMainThread(String event) {
        super.onEventMainThread(event);
        if ("已购买".equals(event) || "收货成功".equals(event)){
            List<OrderBean> orderBeanList = OrderBeanDBUtils.getInstance(getActivity()).getListByUserid(App.getInstance().user.getUser_id(),"0");
            if (orderBeanList ==null || orderBeanList.size() ==0){
                mBinding.rlEmpty.setVisibility(View.VISIBLE);
            }else {
                mBinding.rlEmpty.setVisibility(View.GONE);
                orderAdapter.setNewData(orderBeanList);
                orderAdapter.notifyDataSetChanged();
            }
        }
    }

    @Override
    public void onOrderClick(long id, String address_id) {
        Intent intent = new Intent(getActivity(), OrderDetailActivity.class);
        intent.putExtra("id",id);
        startActivity(intent);

    }
}
